
//
//  YGKJLocationTool.m
//  新项目
//
//  Created by 刘耀宗 on 2016/11/16.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "YGKJLocationTool.h"

#import <AMapFoundationKit/AMapFoundationKit.h>

#import <AMapLocationKit/AMapLocationKit.h>

@interface YGKJLocationTool ()
@property (nonatomic,strong) AMapLocationManager *locationManager;

@end

@implementation YGKJLocationTool

-(AMapLocationManager *)locationManager
{
    if (_locationManager == nil) {
        _locationManager = [[AMapLocationManager alloc] init];
        
        //    self.locationManager.delegate = self;
        // 带逆地理信息的一次定位（返回坐标和地址信息）
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        //   定位超时时间，最低2s，此处设置为11s
        _locationManager.locationTimeout =2;
        //   逆地理请求超时时间，最低2s，此处设置为12s
        _locationManager.reGeocodeTimeout = 2;
    }
    return _locationManager;
    
}
+(instancetype)shareLocationInfo
{
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance=[[self alloc] init];
        [AMapServices sharedServices].apiKey = @"03f21276fbbad07c2780c7aefd6cbb01";
        
    });
    return instance;
    
    
}

-(void)getLoactionInfoSuccess:(void (^)(NSDictionary *result))success
{
    
    
    // 带逆地理（返回坐标和地址信息）。将下面代码中的YES改成NO，则不会返回地址信息。
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        if (error)
        {
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
//        CLLocationCoordinate2D wgsPt = location.coordinate;
//        //火星坐标转换成百度坐标
//        //        CLLocationCoordinate2D gcjPt = [JZLocationConverter gcj02ToBd09:wgsPt];
//        CLLocationCoordinate2D bdPt = [JZLocationConverter gcj02ToBd09:wgsPt];
//        
//        
//        CLLocationDegrees latitude = bdPt.latitude;
//        [User shareInstance].lat=[NSString stringWithFormat:@"%lf",latitude];
//        
//        
//        CLLocationDegrees longitude = bdPt.longitude;
//        [User shareInstance].lng=[NSString stringWithFormat:@"%lf",longitude];
//        
//        [CXBSignalModel shareSignalModel].carAddress=[NSString stringWithFormat:@"%@%@",regeocode.province,regeocode.district];
//        if (regeocode.AOIName.length==0) {
//            [User shareInstance].topicAddress=[NSString stringWithFormat:@"%@%@",regeocode.province,regeocode.district];
//        }else
//        {
//            
//            
//            [User shareInstance].topicAddress=[NSString stringWithFormat:@"%@%@%@",regeocode.province,regeocode.district,regeocode.AOIName];
//            
//        }
//        
        success(nil);
        
        if (regeocode)
        {
            
        }
    }];
    
}
- (void)initCLLocationManager:(UIViewController *)vc anddStr:(NSString *)str
{
    BOOL enable=[CLLocationManager locationServicesEnabled];
    NSInteger status=[CLLocationManager authorizationStatus];
    if(!enable || status<3)
    {
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
        {
            CLLocationManager  *locationManager = [[CLLocationManager alloc] init];
            [locationManager requestAlwaysAuthorization];
            [locationManager requestWhenInUseAuthorization];
        }
        
        //  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开定位开关提供更优质的服务"
        //                                                            message:@"定位服务未开启，请进入系统［设置］> [隐私] > [定位服务]中打开开关，并允许使用定位服务"
        //                                                           delegate:self
        //                                                  cancelButtonTitle:nil
        //                                                  otherButtonTitles:@"取消",@"立即开启", nil];
        
        UIAlertController *alertView=[UIAlertController alertControllerWithTitle:str message:@"定位服务未开启，请进入系统［设置］> [隐私] > [定位服务]中打开开关，并允许使用定位服务" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"立即开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertView addAction:sureAction];
        [alertView addAction:cancelAction];
        
        [vc presentViewController:alertView animated:YES completion:nil];
        
    }
}


@end
